﻿using AJWPFAdmin.Core;
using AJWPFAdmin.Core.Components.AJTable.ViewModels;
using AJWPFAdmin.Core.Components.Views;
using AJWPFAdmin.Core.Excel;
using AJWPFAdmin.Core.ExtensionMethods;
using AJWPFAdmin.Core.Models;
using AJWPFAdmin.Core.Models.EF.Tables;
using AJWPFAdmin.Core.Mvvm;
using AJWPFAdmin.Core.Utils;
using MaterialDesignExtensions.Controls;
using MaterialDesignThemes.Wpf;
using Microsoft.AspNetCore.Mvc;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using AJWPFAdmin.Core.Components.AJFilePicker.ViewModels;
using Prism.Mvvm;
using System.Windows.Media;

namespace AJWPFAdmin.Core.Components.ViewModels
{
    public class AJCommonDialogViewModel : ViewModelBase, IDialogAware
    {
        public class AJCommonDialogParams : BindableBase
        {
            private string _title;
            public string Title
            {
                get { return _title; }
                set { SetProperty(ref _title, value); }
            }

            private bool _showCancel;
            public bool ShowCancel
            {
                get { return _showCancel; }
                set { SetProperty(ref _showCancel, value); }
            }

            private string _message;
            public string Message
            {
                get { return _message; }
                set { SetProperty(ref _message, value); }
            }

            private string _detail;
            public string Detail
            {
                get { return _detail; }
                set { SetProperty(ref _detail, value); }
            }

            private PackIconKind _icon;
            public PackIconKind Icon
            {
                get { return _icon; }
                set { SetProperty(ref _icon, value); }
            }

            private CommonDialogType _type;
            public CommonDialogType Type
            {
                get { return _type; }
                set { SetProperty(ref _type, value); }
            }

            public AJCommonDialogParams()
            {
                Icon = PackIconKind.Alert;
                Type = CommonDialogType.Error;
                ShowCancel = false;
            }
        }

        public event Action<IDialogResult> RequestClose;

        public string Title => "";

        private AJCommonDialogParams _params;
        public AJCommonDialogParams Params
        {
            get => _params;
            set => SetProperty(ref _params, value);
        }

        private DelegateCommand<bool?> _confirmCmd;
        public DelegateCommand<bool?> ConfirmCmd =>
            _confirmCmd ?? (_confirmCmd = new DelegateCommand<bool?>(ExecuteConfirmCmd));

        void ExecuteConfirmCmd(bool? parameter)
        {
            if (!parameter.GetValueOrDefault())
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
                return;
            }

            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
        }

        public AJCommonDialogViewModel()
        {

        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            parameters.TryGetValue<AJCommonDialogParams>("params", out var @params);
            @params ??= new AJCommonDialogParams();
            Params = @params;
        }
    }

    public enum CommonDialogType
    {
        Success,
        Warning,
        Error,
        Info
    }

    public class DetailExpanderVisibleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return string.IsNullOrWhiteSpace(value as string) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class AJCommonDialogTypeToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            SolidColorBrush brush = null;
            if (value is CommonDialogType type)
            {
                switch (type)
                {
                    case CommonDialogType.Success:
                        brush = Application.Current.TryFindResource("AJGreen") as SolidColorBrush;
                        break;
                    case CommonDialogType.Warning:
                        brush = Application.Current.TryFindResource("AJOrange") as SolidColorBrush;
                        break;
                    case CommonDialogType.Error:
                        brush = Application.Current.TryFindResource("AJRed") as SolidColorBrush;
                        break;
                    case CommonDialogType.Info:
                        brush = Application.Current.TryFindResource("AJCyan") as SolidColorBrush;
                        break;
                    default:
                        break;
                }
            }
            return brush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
